<?php
class control extends base
{
	function control()
	{
		$this->base();
		getgpc('user_id','S')?'':exit('无权限');
		$this->load_class('comment');
		$this->load_class('photo');
		$this->load_class('album');
	}
	//发送短信
	function actionpost()
	{		
		$_POST =!empty($_POST)?escape($_POST):'';
		if(getgpc('user_id','S')==null)
		{
				$this->redirect('index.php?m=user&a=login',4000,'请先登录再操作');
				exit();			
		}
		if(!$_POST['pcomment'])
			{
				$this->redirect('index.php?m=photo&a=photo_view&photoid='.$photoid,4000,'评论内容不能为空');
				exit();
			}
		if($_POST['idtype']==1)
		{
			$id = intval($_POST['photoid']);
			$idtype = intval($_POST['idtype']);
			$uid = $_ENV['photo']->get_uid($id);//被评论者ID
			$update = $_ENV['photo'];
			$row2 = array('pcomment_num'=>'pcomment_num+1');
			$url = 'index.php?m=photo&a=photo_view&photoid='.$id;
		}
		elseif($_POST['idtype']==2)
		{
			$id = intval($_POST['albumid']);
			$idtype = intval($_POST['idtype']);
			$uid = $_ENV['album']->get_uid($id);
			$update = $_ENV['album'];
			$row2 = array('acomment_num'=>'acomment_num+1');
			$url = 'index.php?m=album&a=view&albumid='.$id;
		}
		elseif($_POST['idtype']==3)
		{
			$uid=$id = intval($_POST['userid']);
			$idtype = intval($_POST['idtype']);
			$url = 'index.php?m=profile&a=view&userid='.$uid;
			$ctime = time();		
			$authorid = intval($_SESSION['user_id']);//评论者ID
			$username = $_SESSION['user_name'];
			$pcomment = mb_substr($_POST['pcomment'],0,200,'utf-8');
			$row = array(
			'uid'=>"$uid",
			'id'=>"$id",
			'idtype'=>"$idtype",
			'authorid'=>"$authorid",
			'author'=>"$username",
			'created'=>"$ctime",
			'message'=>"$pcomment"
			);
			$result = $_ENV['comment']->post($row);
			if($result)
			{
				$this->redirect($url,4000,'评论提交成功!');
				exit();
			}	
		}
		$ctime = time();		
		$authorid = intval($_SESSION['user_id']);//评论者ID
		$username = $_SESSION['user_name'];
		$pcomment = mb_substr($_POST['pcomment'],0,200,'utf-8');
		$row = array(
		'uid'=>"$uid",
		'id'=>"$id",
		'idtype'=>"$idtype",
		'authorid'=>"$authorid",
		'author'=>"$username",
		'created'=>"$ctime",
		'message'=>"$pcomment"
		);
		$result = $_ENV['comment']->post($row);
		if($result)
		{
			$update->update($row2,$id);
			$this->redirect($url,4000,'评论提交成功!');
			exit();
		}
		else
		{
			$this->redirect('index.php',4000,'评论提交失败,请重试!');
			exit();			
		}
	}
	function actiondelete()
	{
		$cid =is_numeric(getgpc('cid','G'))?intval(getgpc('cid','G')): NULL;
		$id = is_numeric(getgpc('id','G'))?intval(getgpc('id','G')): NULL;
		$idtype = is_numeric(getgpc('idtype','G'))?intval(getgpc('idtype','G')): NULL;
		$uid = getgpc('user_id','S');
		if($id && $cid && $idtype )
		{
			$result = $_ENV['comment']->delete($cid,$uid,$id);
			if($result)
			{
				if($idtype==1)
				{
					$row = array('pcomment_num'=>'pcomment_num-1');
					$_ENV['photo']->update($row,$id);
					$this->redirect('index.php?m=photo&a=photo_view&photoid='.$id,4000,'删除成功');
					exit();
				}		
				elseif($idtype==2)
				{
					$row = array('acomment_num'=>'acomment_num-1');
					$_ENV['album']->update($row,$id);
					$this->redirect('index.php?m=album&a=view&albumid='.$id,4000,'删除成功');
					exit();
				}
			$this->redirect('index.php?m=profile&a=view',4000,'删除成功');
			}
					
		}
		else
		{
			exit('无效操作');
		}
	}
}
?>